#include <QtGui>
#include "devtypedlg.h"
#include "../core/aprcfg.h"

DevTypeSel::DevTypeSel(bool modifiable, const QString& strDev, QWidget *parent)
    :QDialog(parent)
{
    cmbDevType = new QComboBox;
//    QStringList strListDevType;
    strListDevType = new QStringList;
	AprCfg::getNamesByAttr(PM_DEV_ATTR, *strListDevType);
    cmbDevType->addItems(*strListDevType);
	int index = cmbDevType->findText(strDev);
	cmbDevType->setCurrentIndex(index);
//    devTypeIndex = 0;

/*
    btnOk     = new QPushButton(tr("OK"));
    btnCancel = new QPushButton(tr("CANCEL"));
    btnOk->setEnabled(modifiable);
*/

	buttonBox = new QDialogButtonBox;
	QPushButton* btnOk = buttonBox->addButton(tr("OK"), QDialogButtonBox::ActionRole);
	btnOk->setEnabled(modifiable);
	buttonBox->addButton(tr("CANCEL"), QDialogButtonBox::RejectRole);

    tableChInfo = new QTableWidget(TABLE_CHINFO_ROWS, TABLE_CHINFO_COLS);
    for(int r = 0; r < TABLE_CHINFO_ROWS; ++r){
        tableChInfo->setVerticalHeaderItem(r,
                           new QTableWidgetItem(tr("CH")+QString::number(r+1)));
    }
    tableChInfo->horizontalHeader()->setVisible(false);
    tableChInfo->verticalHeader()->setEnabled(false);
    tableChInfo->setColumnWidth(0, 200);

    QHBoxLayout *hlayout = new QHBoxLayout;
    hlayout->addWidget(cmbDevType);
    hlayout->addStretch();
 //   hlayout->addWidget(btnOk);
 //   hlayout->addWidget(btnCancel);
	hlayout->addWidget(buttonBox);

    QVBoxLayout *vlayout = new QVBoxLayout;
    vlayout->addLayout(hlayout);
    vlayout->addWidget(tableChInfo);

    setLayout(vlayout);
    setWindowTitle(tr("DEVICE TYPE"));

   // setFixedSize(260 ,300);

    QObject::connect(btnOk, SIGNAL(clicked()), this, SLOT(selFinished()));
 //   QObject::connect(btnCancel, SIGNAL(clicked()), this, SLOT(selFinished()));
	QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    QObject::connect(cmbDevType,
           SIGNAL(currentIndexChanged(int)), this, SLOT(devTypeChanged(int)));
}

void DevTypeSel::selFinished()
{
	accept();
/*
    QPushButton *btn = qobject_cast<QPushButton *>(sender());

    if(btn == btnOk){
        accept();
    }else if(btnCancel == btn){
        reject();
    }
*/
}

void DevTypeSel::devTypeChanged(int index)
{
//    switch(index){
//    case 0:
//    case 1:
//        case
//    }
    //qDebug("%d", index);
//    devTypeIndex = index;
    QString str = strListDevType->at(index);
	currentDev = str;
    str = str.left(1);
    chnNum = str.toInt();

    //qDebug("%d", chnNum);
//    return;
}
